package com.GatherFour;

import java.io.IOException;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class GameLobby extends Activity {
	private Button send_btn;
	private EditText edText;
	private TextView chatbox, player1, player2, player3, player4;
	private Player user;
	private String hostAddr = "192.168.1.103", clientAddr = "192.168.1.105";
	private Socket outSocket, client, host;
	private ServerSocket inSocket;
	private String inMessage, outMessage;
	
	@Override
	public void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
			
		setContentView(R.layout.game_lobby);
		send_btn = (Button)findViewById(R.id.sendText);
		edText = (EditText)findViewById(R.id.inputText);
		player1 = (TextView)findViewById(R.id.player1);
		player2 = (TextView)findViewById(R.id.player2);
		chatbox = (TextView)findViewById(R.id.chatbox);
		chatbox.setText("");
		
		Bundle bundle = this.getIntent().getExtras();
		if(bundle.getString("self").equals("host")){
			//clientAddr = bundle.getString("clientAddr");
			player1.setText("Host Player");
			player2.setText(clientAddr);
			new Thread(new Runnable(){

				@Override
				public void run() {
					// TODO Auto-generated method stub
					try {
						inSocket = new ServerSocket(8988);
						while(true){
							client = inSocket.accept();
							Scanner in = new Scanner(client.getInputStream());
							if(in.hasNextLine()){
								inMessage = in.nextLine();
								runOnUiThread(updateChatbox);
							}
						}
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
				
			}).start();
			hostInit();
			
		}else{
			//hostAddr = bundle.getString("hostAddr");
			player2.setText("Client Player");
			player1.setText(hostAddr);
			new Thread(new Runnable(){

				@Override
				public void run() {
					// TODO Auto-generated method stub
					try {
						inSocket = new ServerSocket(8989);
						while(true){
							client = inSocket.accept();
							Scanner in = new Scanner(client.getInputStream());
							if(in.hasNextLine()){
								inMessage = in.nextLine();
								runOnUiThread(updateChatbox);
							}
						}
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
				
			}).start();
			clientInit();
		}
		
	//	manager.createGroup(channel, null);

		
	}
	
	public void hostInit(){
		send_btn.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				outMessage = edText.getText().toString();
				chatbox.append("\n"+outMessage);
				
				edText.setText("");
				new Thread(new Runnable(){

					@Override
					public void run() {
					try {
						outSocket = new Socket(clientAddr,8989);
						OutputStream out = outSocket.getOutputStream();
						out.write(outMessage.getBytes());
						outSocket.close();
					} catch (UnknownHostException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					}
				}).start();
				
			}
			
		});
	}
	
	public void clientInit(){
		send_btn.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				outMessage = edText.getText().toString();
				chatbox.append("\n"+outMessage);
				
				edText.setText("");
				new Thread(new Runnable(){
					@Override
					public void run() {
					try {
						outSocket = new Socket(hostAddr,8988);
						OutputStream out = outSocket.getOutputStream();
						out.write(outMessage.getBytes());
						outSocket.close();
					} catch (UnknownHostException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}).start();
				
			}
			
		});

	}
	
	public Runnable updateChatbox = new Runnable(){

		@Override
		public void run() {
			// TODO Auto-generated method stub
			chatbox.append("\n"+inMessage);
		}
		
	};
}
